<?php
/**
 * Created by PhpStorm.
 * User: peter
 * Date: 2018/6/4
 * Time: 下午4:18
 */

namespace app\model;


use extend\STATUS_CODE;

class Goods extends Base
{
    protected $pk   = 'id';
    protected $json = ['images'];
    public $table   = 'goods';
    /**
     * @purpose 管理附表(goods_attach) 查询列表时用使用
     * @return \think\model\relation\HasOne
     */
    public function attachItems () {
        return $this->hasOne('GoodsAttach','goods_id','id');
    }
    /**
     * @purpose 管理附表(goods_attach) 查询详情时使用
     * @return \think\model\relation\HasOne
     */
    public function attach () {
        return $this->hasOne('GoodsAttach','goods_id','id')
            ->bind('article,views,comments,collection,shares,parameter');
    }

    /**
     * @purpose 绑定关联属性表
     * @return \think\model\relation\HasMany
     */
    public function attribute () {
        return $this->hasMany('GoodsAttribute','goods_id','id');
    }

    /**
     * @purpose 绑定关联属性值表
     * @return mixed
     */
    public function attributeAttach ()
    {
        return $this->hasMany('GoodsAttributeAttach', 'goods_id', 'id');
    }
    /**
     * @purpose 通过ID获取单挑数据
     * @param int $id
     * @param string $fileds
     * @return array|null|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function infoById (
        int $id,
        string $fields = 'id,title,thumb,images,pinyin,price,stock,status,category,brand,brand_name,category_name,create_at'
    )
    {
        return $this
            ->with('attach')
            ->where('id', $id)
            ->field($fields)
            ->find();
    }

    /**
     * @purpose 获取数据列表
     * @param array|string $where
     * @param int $page
     * @param int $size
     * @param string|array $order
     * @param string $fields
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function items (
        $where = [],
        int $page  = 1,
        int $size  = 50,
        $order =  'create_at desc',
        string $fields = 'id,title,thumb,images,pinyin,price,stock,status,category,category_name,brand,brand_name,create_at'
        )
    {
        $total  = $this->total($where);
        $result = $this
            ->with('attach')
            ->where($where)
            ->order($order)
            ->field($fields)
            ->page($page, $size)
            ->select();
        return [
            'total' => $total,
            'list'  => $result
        ];
    }

    /**
     * @purpose 获取数据条数
     * @param array $where
     * @return int
     */
    public function total (array $where = []): int
    {
        return (int) $this
            ->where($where)
            ->count();
    }

    /**
     * @purpose 通过ID更新商品状态
     * @param int $id
     * @param int $status
     * @return bool
     */
    public function updateStatusById (int $id, int $status): bool
    {
        return (bool) $this
            ->save(['status' => $status], ['id' => $id]);
    }
}